package com.epro.dx.task;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.epro.dx.listenner.HotWordsAsyncTaskListenner;
import com.weipu.common.facade.content.api.HotWordService;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.factory.ProviderFactory;

/**
 * 获取热搜词汇
 * @author huangfei
 *
 */
public class HotWordsAsyncTask extends AsyncTask<Void,Void,String> {
    private String[] hotWords;
	private HotWordsAsyncTaskListenner myAsyncTaskListenner;
	private Context context;
	public HotWordsAsyncTask(Context context,HotWordsAsyncTaskListenner myAsyncTaskListenner){
		this.myAsyncTaskListenner = myAsyncTaskListenner;
		this.context=context;
	}
	@Override
	protected void onPreExecute() {
		if(null!=myAsyncTaskListenner){
			myAsyncTaskListenner.onPreTask();
		}
	}
	@Override
	protected String doInBackground(Void... params) {
		HotWordService hwService=ProviderFactory.createHotWordServiceDBProvider();
		String[] hots= hwService
				.selectMenusList(context, null);
		if(null!=hots&&null!=myAsyncTaskListenner){
			myAsyncTaskListenner.onFinishSomeTask(hots);
		}
		
		try {
			hotWords= ProviderFactory
			.createSearchService().getHotSearchWords();
			if(null!=hotWords){
				hwService.clearHotWordData();
				hwService.save(context, hotWords);
			}
		} catch (DxException e) {
			return e.getErrCode();
		}
		return "0";
	}
	@Override
	protected void onPostExecute(String result) {
		if(null!=myAsyncTaskListenner){
			myAsyncTaskListenner.onPostTask(result, hotWords);
		}
	}

}
